$(document).ready(function(){
    
    $('#selectAlbum').click(function(){       
        if($('#selectAlbum').text()=="Chọn album hiện có")
        {           
            $(this).html("Tạo album mới");            
            $("#album_name").hide();
            $("#album_id").css("display","inline");          
        }else
        {
            $(this).html("Chọn album hiện có"); 
            $("#album_id").hide();
            $("#album_name").css("display","inline");           
        }
        
    });
    if($('#images').length>0&&$('#imagesUploader').length<=0){
        $('#images').uploadify({
            'uploader'  : '/js/uploadify/uploadify.swf',
            'script'    : '/js/uploadify/image.php',
            'cancelImg' : '/js/uploadify/delete.png',
            'buttonImg' : '/js/uploadify/BrowseButton.png',
            'fileExt'   : '*.jpg;*.gif;*.png;*.jpeg;*.bmp',
            'multi'     : true,
            'queueSizeLimit' : 20,
            'sizeLimit':1024000,
            'auto'      : false,
            'removeCompleted' : false,
            'width'      : 150,
            'height'     : 24,
            'queueID'    : 'imagesList',
            'scriptData'  : {
                'mid':$("#mid").val()                        
            },
            'onSelect' : function(event,ID,fileObj,data) {
                $(".loading").show();
            },
            'onCancel': function(event,ID,fileObj,data) {
                var image_id=$("#images"+ID).attr('rel');
                if(image_id>0)
                {
                    $.ajax({
                        type: "POST",
                        url: "/process.php",
                        data: "action=delete_image&image_id="+image_id,
                        success: function(msg){
                       
                        }
                    });
                }
                if($('#imagesList .uploadifyQueueItem').length<=1){
                    $('#share_album').removeClass("btnBlogComment");
                    $('#share_album').addClass("button_disable");
                    $('#share_album').attr('disabled','disabled');
                }
                
            },
            'onAllComplete' : function(event,data) {              
                if(data.filesUploaded>0 ){
                    $('#share_album').removeClass("button_disable");
                    $('#share_album').addClass("btnBlogComment");
                    $('#share_album').removeAttr('disabled');
                }
                $(".loading").hide();
                $('.editable').hide();
                $('.readonly').show();
                
            },
            'onComplete'  : function(event, ID, fileObj, response, data) { 
                var jsonObject = jQuery.parseJSON(response);
                $("#images"+ID+" .upl_loading").remove();
                $('#images'+ID).prepend('<div><img id="img_'+ID+'" src="'+jsonObject.thumb+'" style="width: 130px; height: 100px; top: 0px; left: 0px;"></div>');
                $('#images'+ID).attr("rel",jsonObject.id)
              
            },
            'onError': function(event,ID,fileObj,errorObj){
                $('.editable').hide();
                $('.readonly').show();
            }
            ,
            'onSelectOnce':function(event,data)
            {
                var aname="";
                if($("#aid").val()=="")
                {
                    if($('#selectAlbum').text()=="Chọn album hiện có")
                    {
                        var $share_for="|";
                        $(".search-choice").each(function(index){
                            if(!$(this).attr("rel").empty&&!$(this).attr("rel")!="undefined")
                            {
                                $share_for+=$(this).attr("rel")+"|";
                            }
                        });
                        var data = "";
                        var name = $("#album_name").val();
                        data = data+"&name="+name;
                        data = data+"&share_for="+$share_for;
                        $.ajax({
                            type: "POST",
                            url: "/process.php",
                            data: "action=create_album"+data,
                            dataType: 'json',
                            success: function(msg){
                                if(msg.status=='name_empty'){
                                    alert("Bạn chưa nhập tên album!");
                                }
                                if(msg.status=='ok'){ 
                                    $("#album_name_readonly").html($('#album_name').val());                                   
                                    $("#aid").val(msg.album_id);                                    
                                    $('#images').uploadifySettings('scriptData',{
                                        'album': $("#aid").val()
                                    });
                                    $('#images').uploadifyUpload();
                                    var data = "";
                                    data = data+"&aname="+$('#album_name').val();
                                    data = data+"&aid="+$("#aid").val();
                                    data = data+"&share_for="+$share_for;
                                    $.ajax({
                                        type: "POST",
                                        url: "/process.php",
                                        data: "action=album_notify"+data,
                                        dataType: 'json',
                                        success: function(msg){
                                            if(msg.status=='ok'){

                                        }
                                        }
                                    });
                                }
                            }
                        });
                    
                    }
                    else
                    {
                        $("#album_name_readonly").html($("#album_id :selected").text());
                       
                        $("#aid").val($("#album_id").val());
                        $('#images').uploadifySettings('scriptData',{
                            'album':$("#album_id").val()
                        });
                        $('#images').uploadifyUpload();
                        var data = "";
                        data = data+"&aname="+$("#album_id :selected").text();
                        data = data+"&aid="+$("#aid").val();
                        data = data+"&share_for="+$share_for;
                        $.ajax({
                            type: "POST",
                            url: "/process.php",
                            data: "action=album_notify"+data,
                            dataType: 'json',
                            success: function(msg){
                                if(msg.status=='ok'){

                            }
                            }
                        });
                    }
                }
                else
                {
                    $('#images').uploadifySettings('scriptData',{
                        'album':$("#aid").val()
                    });
                    $('#images').uploadifyUpload();
                }
                         
            }
        });
    }

    $('#share_album').click(function(){
        var $share_for="|";
        $(".search-choice").each(function(index){
            if(!$(this).attr("rel").empty&&!$(this).attr("rel")!="undefined")
            {
                $share_for+=$(this).attr("rel")+"|";
            }
        });

        var data = "";
        var aid = $("#aid").val();
        data = data+"&aid="+aid;
        data = data+"&share_for="+$share_for;
        $.ajax({
            type: "POST",
            url: "/process.php",
            data: "action=share_album"+data,
            dataType: 'json',
            success: function(msg){        
                if(msg.status=='ok'){
                    window.parent.location.reload();
                }
            }
        });
    });
   

    
});
